﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using FazaDemo.WebSiteMvc.Controllers;
using FazaDemo.BL.AppFacade;
using FazaDemo.ViewModels;
using FazaDemo.BL.SubDomains;
using FazaDemo.PL.Mapping;
using System.Web.Mvc;


namespace FazaDemo.WebSiteMvc.Tests
{
    /// <summary>
    /// Summary description for NavigatorControllerTest
    /// </summary>
    [TestClass]
    public class NavigatorControllerTest
    {

        private const int NumEcq = 1062;

        private NavigatorCondition _defNavCond;

        private IEnumerable<NavElementViewModel> _defNavElemEnum;

        private NavPageViewModel _defPageViewModel;

        private Func<NavigatorController, ActionResult> _page;

        private NavigatorController _controller;

        [TestInitialize]
        public void TestInit()
        {
            _defNavCond = new NavigatorCondition { CondElements = new List<NavCondElement>() };
            _defNavElemEnum = new List<NavElementViewModel>();
            _defPageViewModel = new NavPageViewModel();
            _page = PageFunc;
        }

        [TestMethod]
        public void PageDependenciesBehaviorVerificationTest()
        {
            //Arrange
            var mockRep = new MockRepository(MockBehavior.Loose);
            var providerMock = mockRep.Create<INavCondProvider>();
            providerMock.Setup(p => p.GetNavCondition(It.IsAny<int>())).Returns<int>(p => _defNavCond);
            var contMapperMock = mockRep.Create<IContainerElementMapper<NavCondElement, NavElementViewModel>>();
            contMapperMock.Setup(m => m.MapFrom(It.IsAny<IEnumerable<NavCondElement>>()))
                .Returns<IEnumerable<NavCondElement>>(m => _defNavElemEnum);
            var mapperMock = mockRep.Create<ILowToTopMapper<NavigatorCondition, NavPageViewModel>>();
            mapperMock.Setup(m => m.MapFrom(It.IsAny<NavigatorCondition>()))
                .Returns<NavigatorCondition>(m => _defPageViewModel);
            _controller = new NavigatorController(providerMock.Object, contMapperMock.Object, mapperMock.Object);
            //Act
            _page(_controller);
            //Assert
            mockRep.Verify();
        }

        [TestMethod]
        public void PageTest()
        {
            //Arrange
            var providerDep = Mock.Of<INavCondProvider>(p => p.GetNavCondition(It.IsAny<int>()) == _defNavCond);
            var contMapperDep = Mock.Of<IContainerElementMapper<NavCondElement, NavElementViewModel>>
                (m => m.MapFrom(It.IsAny<IEnumerable<NavCondElement>>()) == _defNavElemEnum);
            var mapperDep = Mock.Of<ILowToTopMapper<NavigatorCondition, NavPageViewModel>>
                (m => m.MapFrom(It.IsAny<NavigatorCondition>()) == _defPageViewModel);
            _controller = new NavigatorController(providerDep, contMapperDep, mapperDep);
            //Act
            var result = _page(_controller) as ViewResult;
            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(result.Model.GetType(), typeof(NavPageViewModel));
        }

        private ActionResult PageFunc(NavigatorController controller)
        {
            return controller.Page(NumEcq);
        }

    }
}
